<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="ThemeBucket">
    <link rel="shortcut icon" href="#" type="image/png">

    <title>Login</title>

    <link href="../static/css/style.css" rel="stylesheet">
    <link href="../static/css/style-responsive.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="../static/js/html5shiv.js"></script>
    <script src="../static/js/respond.min.js"></script>
    <![endif]-->
</head>

<body class="login-body">

<div class="container">

    <form class="form-signin" id="login_form" >
        {% csrf_token %}
        <div class="form-signin-heading text-center">
            <h1 class="sign-title">登 录</h1>
            <img src="../static/images/login-logo.png" alt=""/>
        </div>
        <div class="login-wrap">
            <input type="text" class="form-control" placeholder="用户名" autofocus name="username" id="username">
            <input type="password" class="form-control" placeholder="密码" name="pwd" id="pwd">

            <button class="btn btn-lg btn-login btn-block" type="button" id="submit_btn" disabled>
                <i class="fa fa-check"></i>
            </button>

            <div class="registration">
                还未是会员?
                <a class="" href="registration.html">
                    注册
                </a>
            </div>
            <label class="checkbox">
                <input type="checkbox" value="remember-me"> 记住我
                <span class="pull-right">
                    <a data-toggle="modal" href="#myModal"> 忘记密码？</a>

                </span>
            </label>

        </div>

        <!-- Modal -->
        <div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="myModal" class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title">Forgot Password ?</h4>
                    </div>
                    <div class="modal-body">
                        <p>Enter your e-mail address below to reset your password.</p>
                        <input type="text" name="email" placeholder="Email" autocomplete="off" class="form-control placeholder-no-fix">

                    </div>
                    <div class="modal-footer">
                        <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
                        <button class="btn btn-primary" type="button">Submit</button>
                    </div>
                </div>
            </div>
        </div>
        <!-- modal -->

    </form>

</div>

<!-- Placed js at the end of the document so the pages load faster -->

<!-- Placed js at the end of the document so the pages load faster -->
<script src="../static/js/jquery-1.10.2.min.js"></script>
<script src="../static/js/bootstrap.min.js"></script>
<script src="../static/js/modernizr.min.js"></script>
<script>

   function showErrCss(id,msg){
      $(id).css("border","2px solid red");
      $("#submit_btn").attr("disabled",true);
      $(id).val("");
      $(id).attr("placeholder",msg);
   }

   $().ready(function(){
        var username_state = false;
        var passwd_state = false;

        $("#username").blur(function(){
            if($(this).val() == ''){
                showErrCss(this,"用户名为空，请重新输入");
            }else{
                $(this).css("border","1px solid #eaeaec");
                username_state = true;
                if(passwd_state){
                    $("#submit_btn").attr("disabled",false);
                }
            }
        });
        $("#pwd").blur(function(){
            if($(this).val() == '' ){
               showErrCss(this,"密码为空，请重新输入");
            }else if(! /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,8}$/.test($(this).val())){
                showErrCss(this,"密码格式有误，要求6-8位字母+数字");
            }else{
                $(this).css("border","1px solid #eaeaec");
                passwd_state = true;
                if(username_state){
                    $("#submit_btn").attr("disabled",false);
                }
            }
        });

        $("#submit_btn").click(function(){
            $.post("/login/",
                    $("#login_form").serialize(),
                    function(data,statu){
                        if(data.code == 101){
                            window.location.href = "/index/"
                        }else if(data.code == 102){
                            showErrCss("#username",data.msg)
                        }else if(data.code == 103){
                            showErrCss("#pwd",data.msg)
                        }
                    });
        });
   });
</script>

</body>
</html>
